﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using ThoiTrang.Entities;
using ThoiTrang.Business;

public partial class UserControls_LastestArticles : System.Web.UI.UserControl
{
    private int articleCount;
    public int ArticleCount
    {
        set { articleCount = value; }
        get { return articleCount; }
    }


    protected override void OnInit(EventArgs e)
    {
        rptLastestArticles.ItemDataBound += new RepeaterItemEventHandler(rptLastestArticles_ItemDataBound);
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadLastestArticles();
    }

    protected void rptLastestArticles_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            a_Article art = e.Item.DataItem as a_Article;
            HtmlAnchor lnkArticle = e.Item.FindControl("lnkArticle") as HtmlAnchor;
            lnkArticle.InnerHtml = TTHelper.GetArticleTitle(art.UTitle);
            lnkArticle.Title = art.UTitle;
            lnkArticle.HRef = ResolveUrl(String.Format("~/detail.aspx?{0}={1}&{2}={3}", FOConstants.QSP_AID, art.ID, FOConstants.QSP_CKID, SessionManager.GetCookie()));
        }
    }

    protected void LoadLastestArticles()
    {
        try
        {
            //int total = 0;
            //List<a_Article> lastArts = a_ArticleService.Geta_ArticleList(a_ArticleColumns.ID, ThoiTrang.Data.OrderDirection.DESC, 1, ArticleCount, out total);
            List<a_Article> lastArts = (List<a_Article>)Cache["lastArts"];//a_ArticleService.GetLastestArticles();
            if (lastArts == null)
            {
                lastArts = a_ArticleService.GetLastestArticles();
                Cache.Insert("lastArts", lastArts, null, DateTime.Now.AddHours(FOConstants.ExpireCache), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            rptLastestArticles.DataSource = lastArts;
            rptLastestArticles.DataBind();            
        }
        catch (Exception ex)
        {
        }
    }
}
